package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.i18n.model.LocaleResourceTL;
import cn.b.sky.i18n.model.SystemLanguageInfo;
import cn.b.sky.i18n.service.I18nService;
import cn.b.sky.i18n.service.LocaleResourceTLService;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/localeResourceTL/*")
@Slf4j
public class LocaleResourceTLController extends SecurityController {

  @Resource
  private LocaleResourceTLService localeResourceTLService;

  @Autowired
  private I18nService i18nService;

  @RequestMapping("conditionPage")
  public String conditionlocaleResource(String navTabId, Model model, String resourceSet, String resourceKey,
                                        String sourceLanguage, String targetLanguage) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("resourceSet", resourceSet);
    model.addAttribute("resourceKey", resourceKey);
    model.addAttribute("sourceLanguage", sourceLanguage);
    model.addAttribute("targetLanguage", targetLanguage);
    List<SystemLanguageInfo> languages = i18nService.listLanguage();
    model.addAttribute("languages", languages);
    return "i18n/localeResourceTL_Condition";
  }

  @RequestMapping("localeResourceTL_list")
  public String listLocaleResourceTL(Model model, String navTabId, String resourceSet, String resourceKey,
                                     String sourceLanguage, String targetLanguage) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("resourceSet", resourceSet);
    model.addAttribute("resourceKey", resourceKey);
    model.addAttribute("sourceLanguage", sourceLanguage);
    model.addAttribute("targetLanguage", targetLanguage);
    List<SystemLanguageInfo> languages = i18nService.listLanguage();
    for (SystemLanguageInfo languageInfo : languages) {
      if (languageInfo.getLanguageCode().equals(sourceLanguage)) {
        model.addAttribute("sourceLanguageText", languageInfo.getLanguageName());
      }
      if (languageInfo.getLanguageCode().equals(targetLanguage)) {
        model.addAttribute("targetLanguageText", languageInfo.getLanguageName());
      }
    }
    return "i18n/localeResourceTL_list";
  }

  @RequestMapping("getlocaleResourceTL")
  @ResponseBody
  public DWZResult getLocaleResourceTL(String resourceSet, String resourceKey, String sourceLanguage,
      String targetLanguage) {
    return ajaxDone(localeResourceTLService.getlocaleResourceTLByCode(this.getSiteCode(), resourceSet, resourceKey,
        sourceLanguage, targetLanguage));
  }

  @RequestMapping("savelocaleResourceTL")
  @ResponseBody
  public DWZResult saveLocaleResourceTL(String localeResourceTLs, String targetLanguage) {
    try {
      List<LocaleResourceTL> list = JSONArray.parseArray(localeResourceTLs, LocaleResourceTL.class);
      localeResourceTLService.saveLocaleResourceTLlist(this.getSiteCode(), list, targetLanguage);
      return ajaxDone(null);
    } catch (Exception e) {
      log.error("{}", e);
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.DATABASE_OPERATION_FAIL));
    }
  }
}
